package com.wsy.servlet;

import com.wsy.pojo.Attendence;
import com.wsy.service.AttendenceService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/index.html")
public class AttendenceServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String staffName =req.getParameter("staffName");

        ServletContext app=this.getServletContext();
        ApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(app);
        AttendenceService attendenceService= (AttendenceService) ac.getBean("attendenceService");
        List<Attendence> list=null;
        String name=null;
        if(staffName!=null&&staffName.equals("")){
            name=staffName;
            list=attendenceService.getAll1(name);
        }else {
            list=attendenceService.getAll();
        }


        req.setAttribute("name",name);
        req.setAttribute("list",list);

        req.getRequestDispatcher("index.jsp").forward(req,resp);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
